<?PHP
session_start();
/* $Id: index.php 7 2009-03-24 20:25:52Z ronan $ */
//Modified by Slyferz

//getting the user's access to see if they are allowed
include("includes/paxsql.php");
mysql_close();

include 'includes/database.php';
include 'includes/library.php';

function getName()
{
	//$charID = 1240402153;
	$charID = addslashes($_GET["id"]);
	$result = mysql_query("SELECT name FROM characters WHERE charID = '".$charID."'");
	$row = mysql_fetch_array($result);
	echo($row['name']);
}

function getChar($charID)
{
	$query = "SELECT name, charID, lastSeen, inactive, joined
		FROM characters WHERE charID = '".$charID."'";
	$result = mysql_query($query);
	$char = mysql_fetch_array($result);
	return $char;
}

function listPeople() {
	$charID = addslashes($_GET["id"]);
	//$charID = 1240402153;
	$char = getChar($charID);

	$result = mysql_query("SELECT DISTINCT characters.name, characters.lastSeen, alts.altOf
		FROM characters JOIN alts 
		ON characters.charID = alts.altOf
		WHERE alts.charID = '".$charID."'");
	$main = mysql_fetch_array($result);
	$mainID = $main['altOf'];

	$query2 = "SELECT distinct characters.charID, characters.name, characters.lastSeen
		FROM characters JOIN alts
		ON alts.charID = characters.charID
		WHERE alts.altOf = '{$charID}'
		AND characters.inCorp = 1
		ORDER BY characters.name";

	echo "<table class=\"main\"><tr><td class=\"main\" width=\"70%\"><b>Name</b></td><td class=\"main\" width=\"30%\"><b>Last Seen</b></td></tr>";
			
	echo "<tr><td class=\"main\">";
	renderName($char['charID']);
	echo "</td>";
	checkTime($char);
	if(empty($mainID))
	{
		echo "<tr><td style = 'font-weight: bold;text-align:center;'>ALTS</td></tr>";

		$result = mysql_query($query2) or die (mysql_error());
		while($alts = mysql_fetch_array($result))
		{
			echo "<tr><td class=\"main\">";
			renderName($alts['charID']);
			echo "</td>";
			checkTime($alts);
		}
	}
	else
	{
		echo "<tr><td style = 'font-weight: bold; text-align: center;'>ALT OF</td></tr>
			<tr><td class = 'main'>";
		renderName($mainID);
		echo "</td>";
		checkTime($main);
	}
	echo("</table>");
}

function checkTime($char)
{
	echo "<td class=\"main\">";
	$timePassed = eveTime() - $char['lastSeen'];
	if ($timePassed < 3600) echo "-" . round($timePassed/60) . " mins";
	else if ($timePassed < 172800) echo "-" . round($timePassed/3600, 2) . " hrs";
	else echo "-" . round($timePassed/86400, 2) . " days";
	echo "</td></tr>";
}

if($access > 1)
{
echo("
<html>
<head>
<link rel='stylesheet' type='text/css' href='main.css'>
<title>snowflake</title>");

include 'includes/javascript.php';
echo("
</head>

<body>

<div id='isAltWin' style='display: none;'></div>
<div id='flagWin' style='display: none;'></div>

<div id='wrapper'>

	<div id='header'><a href='/'>snowflake</a></div>

	<div id='navi'>");
	include('includes/navi.php');
	echo("
	</div>

	<div id='main'>
		<h1>");
getName();
	
	echo("</h1>");

	$char = getChar(addslashes($_GET["id"]));
	$timePassed = eveTime() - $char['lastSeen'];
	$joined = date("M j, Y", $char['joined']);
	$joinedPassed = round((eveTime() - $char['joined'])/86400, 2);
	echo "<table class=\"cleanTable\">";
	echo "Joined {$joined} ({$joinedPassed} days ago)<br>";
	if ($timePassed < 3600) echo "Last seen " . round($timePassed/60) . " mins ago.";
	else if ($timePassed < 172800) echo "Last seen " . round($timePassed/3600, 2) . " hrs ago.";
	else echo "Last seen " . round($timePassed/86400, 2) . " days ago.";
	if ($char['inactive'] == 1) echo " Character is inactive.";
	echo "<br><br>";

	echo("<hr>");
listPeople();
	echo("</div>
	
</div>

</body>

</html>");
}
//they do not belong here, they get the boot
else
{
	header("Location: http://".$_SERVER['HTTP_HOST']."/index.php");
}
mysql_close();
?>
